package cn.com.thingo.hyintelligent.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.suke.widget.SwitchButton;
import com.th.opensdk.openapi.DimmerInfo;
import com.th.opensdk.openapi.SwitchInfo;
import com.warkiz.widget.IndicatorSeekBar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.thingo.hyintelligent.R;

public class LiteListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int DIMMER = 1;
    private static final int SWITCH = 2;

    private List<SwitchInfo> switchList;
    private List<DimmerInfo> dimmerList;
    private Context context;

    public LiteListAdapter(List<SwitchInfo> switchList, List<DimmerInfo> dimmerList, Context context) {
        this.switchList = switchList;
        this.dimmerList = dimmerList;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == DIMMER) {
            return new DimmerViewHolder(LayoutInflater.from(context).inflate(R.layout.item_lite_list_dimmer, parent, false));
        } else {
            return new SwitchViewHolder(LayoutInflater.from(context).inflate(R.layout.item_lite_list_switch, parent, false));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position < dimmerList.size()) {
            return DIMMER;
        } else {
            return SWITCH;
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == SWITCH) {
            final SwitchInfo switchInfo = switchList.get(position - dimmerList.size());
            ((SwitchViewHolder) holder).tvName.setText(switchInfo.getDeviceName());
            ((SwitchViewHolder) holder).switchButton.setChecked(switchInfo.getState() == 1);
            ((SwitchViewHolder) holder).switchButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                    if (isChecked) {
                        switchInfo.turnOn();
                    } else {
                        switchInfo.turnOff();
                    }
                }
            });
        } else {
            final DimmerInfo dimmerInfo = dimmerList.get(position);
            ((DimmerViewHolder) holder).tvName.setText(dimmerInfo.getDeviceName());
            ((DimmerViewHolder) holder).seekBar.setProgress(dimmerInfo.getBrightness());
            ((DimmerViewHolder) holder).seekBar.setOnSeekChangeListener(new IndicatorSeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(IndicatorSeekBar seekBar, int progress, float progressFloat, boolean fromUserTouch) {
                    dimmerInfo.turnTo(Byte.valueOf(String.valueOf(progress)));
                }

                @Override
                public void onSectionChanged(IndicatorSeekBar seekBar, int thumbPosOnTick, String tickBelowText, boolean fromUserTouch) {

                }

                @Override
                public void onStartTrackingTouch(IndicatorSeekBar seekBar, int thumbPosOnTick) {

                }

                @Override
                public void onStopTrackingTouch(IndicatorSeekBar seekBar) {

                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return switchList.size() + dimmerList.size();
    }


    static class DimmerViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_lite_dimmer_name)
        TextView tvName;
        @BindView(R.id.item_lite_dimmer_seek_bar)
        IndicatorSeekBar seekBar;


        DimmerViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class SwitchViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_lite_switch_name)
        TextView tvName;
        @BindView(R.id.item_lite_switch_button)
        SwitchButton switchButton;

        SwitchViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
